package haizhi.fruitmall.service.impl;

import com.github.pagehelper.PageHelper;
import haizhi.fruitmall.dao.mapper.GoodsParamJoinMapper;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.GoodsParamJoinForm;
import haizhi.fruitmall.pojo.po.GoodsParamJoin;
import haizhi.fruitmall.pojo.po.GoodsParamJoinExample;
import haizhi.fruitmall.service.GoodsParamJoinService;
import haizhi.fruitmall.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by richa on 2017/9/11
 */

@Slf4j
@Service
@Transactional
public class GoodsParamJoinServiceImpl implements GoodsParamJoinService {

    @Autowired
    private GoodsParamJoinMapper goodsParamJoinMapper;

    @Override
    public void insert(GoodsParamJoinForm form) {
        GoodsParamJoin record = new GoodsParamJoin();
        BeanUtil.copyBean(form,record);
        record.setJoinId(BeanUtil.longId());
        goodsParamJoinMapper.insertSelective(record);
    }

    @Override
    public void update(GoodsParamJoinForm form){
        GoodsParamJoin record = this.getone(form.getJoinId());
        if(BeanUtil.isNull(record)){ throw new ResultException("记录不存在！"); }
        BeanUtil.copyBean(form,record);
        record.setUpdateTime(BeanUtil.newTime());
        goodsParamJoinMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delete(Long id) {
        if(BeanUtil.isNull(this.getone(id))){ throw new ResultException("记录不存在！"); }
        goodsParamJoinMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsParamJoin getone(Long id) {
        if(BeanUtil.isNull(id)){ throw new ResultException("编号为空！");}
        return goodsParamJoinMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsParamJoin> getall(GoodsParamJoinForm form) {
        GoodsParamJoinExample example = new GoodsParamJoinExample();
        GoodsParamJoinExample.Criteria criteria = example.createCriteria();
        if(BeanUtil.noNull(form.getGoodsId())){
            criteria.andGoodsIdEqualTo(form.getGoodsId());
        }
        if(BeanUtil.noNull(form.getUnitId())){
            criteria.andUnitIdEqualTo(form.getUnitId());
        }
        if(BeanUtil.noNull(form.getPageNum()) && BeanUtil.noNull(form.getPageSize())){
            PageHelper.startPage(form.getPageNum(),form.getPageSize());
        }
        return goodsParamJoinMapper.selectByExample(example);
    }

}
